package com.haobangs.imms.service.impl;

import java.util.Arrays;
import java.util.List;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.config.MyLambdaQueryWrapper;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.haobangs.imms.mapper.FactoryMapper;
import com.haobangs.imms.domain.Factory;
import com.haobangs.imms.service.IFactoryService;

/**
 * 工厂管理Service业务层处理
 *
 * @author haobangs
 * @date 2023-06-12
 */
@Service
public class FactoryServiceImpl extends ServiceImpl<FactoryMapper, Factory> implements IFactoryService
{
    @Autowired
    private FactoryMapper factoryMapper;

    /**
     * 查询工厂管理
     *
     * @param id 工厂管理主键
     * @return 工厂管理
     */
    @Override
    public Factory selectFactoryById(Long id)
    {
        return factoryMapper.selectById(id);
    }

    /**
     * 分页查询工厂管理
     *
     * @param factory 工厂管理
     * @return Page 对象
     */
    public Page getByPage(Factory factory){
        MyLambdaQueryWrapper<Factory> wrapper = new MyLambdaQueryWrapper<>();
                    wrapper.likeIfPresent(Factory::getFactoryName, factory.getFactoryName());
                    wrapper.likeIfPresent(Factory::getCustomerName, factory.getCustomerName());
        Page<Factory> factorys = factoryMapper.selectPage(new Page<>(factory.getPageNum(), factory.getPageSize()), wrapper);
        return factorys;
    }

    /**
     * 查询工厂管理列表
     *
     * @param factory 工厂管理
     * @return 工厂管理
     */
    @Override
    public List<Factory> selectFactoryList(Factory factory)
    {
        MyLambdaQueryWrapper<Factory> wrapper = new MyLambdaQueryWrapper<>();
                    wrapper.likeIfPresent(Factory::getFactoryName, factory.getFactoryName());
                    wrapper.likeIfPresent(Factory::getCustomerName, factory.getCustomerName());
        return factoryMapper.selectList(wrapper);
    }

    /**
     * 新增工厂管理
     *
     * @param factory 工厂管理
     * @return 结果
     */
    @Override
    public int insertFactory(Factory factory)
    {
        factory.setCreateBy(getUsername());
        factory.setCreateTime(DateUtils.getNowDate());
        return factoryMapper.insert(factory);
    }

    /**
     * 修改工厂管理
     *
     * @param factory 工厂管理
     * @return 结果
     */
    @Override
    public int updateFactory(Factory factory)
    {
        factory.setUpdateBy(getUsername());
        factory.setUpdateTime(DateUtils.getNowDate());
        return factoryMapper.updateById(factory);
    }

    /**
     * 批量删除工厂管理
     *
     * @param ids 需要删除的工厂管理主键
     * @return 结果
     */
    @Override
    public int deleteFactoryByIds(Long[] ids)
    {
        if(ids.length == 0){
            throw new RuntimeException("id不能为空");
        }
        return factoryMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除工厂管理信息
     *
     * @param id 工厂管理主键
     * @return 结果
     */
    @Override
    public int deleteFactoryById(Long id)
    {
        return factoryMapper.deleteById(id);
    }

    public LoginUser getLoginUser()
    {
        return SecurityUtils.getLoginUser();
    }
    public String getUsername()
    {
        return getLoginUser().getUsername();
    }
}
